package com.javaee.ch11.service;


import com.javaee.ch11.common.exceptions.ServiceException;
import com.javaee.ch11.entity.Book;
import com.javaee.ch11.entity.Reader;
import com.javaee.ch11.mapper.BookMapper;
import com.javaee.ch11.mapper.ReaderMapper;
import com.javaee.ch11.web.dto.BookQueryDto;
import com.javaee.ch11.web.dto.ReaderQueryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class ReaderService {
    @Autowired
    private ReaderMapper readerMapper;

    public List<Reader> listAll() {
        return readerMapper.selectAll();
    }

    public Reader getById(Long id) {
        return readerMapper.selectById(id);
    }

    public void add(Reader reader) {
        if (readerMapper.insert(reader) != 1) {
            throw new ServiceException("读者添加失败");
        }

    }

    public void updateById(Reader reader) {
        if (readerMapper.updateById(reader) != 1) {
            throw new ServiceException("读者更新失败");
        }
    }

    public void deleteById(Long id) {
        if (readerMapper.deleteById(id) != 1) {
            throw new ServiceException("读者删除失败");
        }
    }

    public List<Reader> queryReader(ReaderQueryDto queryDto) {
        return readerMapper.queryReaders(queryDto);
    }
}